package com.yuanjin.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuanjin.R;
import com.yuanjin.util.MediaUtils;
import com.yuanjin.util.NotesDB;

public class NoteAdapter extends BaseAdapter {
	private Context context;
	private Cursor cursor;
	private LinearLayout layout;

	public NoteAdapter(Context context, Cursor cursor) {
		this.context = context;
		this.cursor = cursor;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cursor.getCount();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return cursor.getPosition();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		LayoutInflater inflate = LayoutInflater.from(context);
		if (convertView == null) {
			convertView = inflate.inflate(
					com.yuanjin.R.layout.fragment_note_list_cell, null);
			vh = new ViewHolder();
			vh.tv_content = (TextView) convertView
					.findViewById(R.id.list_content);
			vh.tv_time = (TextView) convertView.findViewById(R.id.list_time);
			vh.iv_img = (ImageView) convertView.findViewById(R.id.list_img);
			vh.iv_video = (ImageView) convertView.findViewById(R.id.list_video);
			// converview与vh建立对应关系
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		cursor.moveToPosition(position);
		String content = cursor.getString(cursor
				.getColumnIndex(NotesDB.CONTENT));
		String time = cursor.getString(cursor.getColumnIndex(NotesDB.TIME));
//		String url = cursor.getString(cursor.getColumnIndex(NotesDB.PATH));
//		String urlvideo = cursor
//				.getString(cursor.getColumnIndex(NotesDB.VIDEO));
		//显示已保存的缩略图
		String url = cursor.getString(cursor.getColumnIndex(NotesDB.PATH_THUMBNAIL));
		String urlvideo = cursor
				.getString(cursor.getColumnIndex(NotesDB.VIDEO_THUMBNAIL));
		Log.e("test", "photo-url=" + url);
		vh.tv_content.setText(content);
		vh.tv_time.setText(time);
		vh.iv_img.setImageBitmap(BitmapFactory.decodeFile(url));
		vh.iv_video.setImageBitmap(BitmapFactory.decodeFile(urlvideo));
		return convertView;

	}

	class ViewHolder {
		private TextView tv_content;
		private TextView tv_time;
		private ImageView iv_img;
		private ImageView iv_video;
	}

}
